package com.cincc.cinccimdemo.ui.main;

import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.cincc.cinccimdemo.R;
import com.cincc.cinccimdemo.common.AttrConstants;
import com.cincc.cinccimdemo.ui.IMBaseActivity;
import com.cincc.cinccimdemo.ui.conversation.ConversationActivity;
import com.cincc.im.callback.ImCallback;
import com.cincc.im.core.IMSipPhoneCtrl;
import com.cincc.im.core.SipCoreUtils;
import com.cincc.im.listener.ImRcvGroupNotifyListener;
import com.cincc.im.model.ImResponse;
import com.cincc.im.model.dbmodel.GroupInfo;
import com.cincc.im.model.enums.ApplyGroupType;
import com.cincc.im.model.notify.group.GroupNotify;
import com.cincc.im.model.notify.notifyenum.GroupNotifyType;
import com.cincc.im.table.IMContactGroup;

public class GroupInfoActivity extends IMBaseActivity implements View.OnClickListener, ImRcvGroupNotifyListener {
    private static final String TAG = "GroupInfoActivity";

    @Override
    protected int contentLayout() {
        return R.layout.activity_group_info;
    }

    ImageView portraitImageView;
    TextView groupNameTextView;
    Button actionButton;

    @Override
    protected void bindViews() {
        super.bindViews();
        portraitImageView = findViewById(R.id.portraitImageView);
        groupNameTextView = findViewById(R.id.groupNameTextView);
        actionButton = findViewById(R.id.actionButton);
    }

    @Override
    protected void bindEvents() {
        super.bindEvents();
        actionButton.setOnClickListener(this);
        SipCoreUtils.getInstance().addImRcvGroupNotifyListener(this);
    }

    String groupId;
    GroupInfo groupInfo;
    IMContactGroup imContactGroup;

    @Override
    protected void afterViews() {
        super.afterViews();
        groupId = getIntent().getStringExtra(AttrConstants.GROUP_ID);
        groupInfo = IMSipPhoneCtrl.getInstance().getGroupInfo(groupId);
        actionButton.setText(groupInfo == null ? "加入群聊" : "进入群聊");
        IMSipPhoneCtrl.getInstance().getGroupInfo(groupId, new ImCallback() {
            @Override
            public void callback(ImResponse imResponse) {
                if (imResponse.getCode() == 0) {
                    imContactGroup = (IMContactGroup) imResponse.getData();
                    runOnUiThread(() -> refreshUI(imContactGroup));
                }
            }
        });
    }

    private void refreshUI(IMContactGroup imContactGroup) {
        groupNameTextView.setText(imContactGroup.name);
        Glide.with(this).load(imContactGroup.protraitUrl).placeholder(R.mipmap.ic_group_chat)
                .transforms(new CenterCrop(), new RoundedCorners(15))
                .into(portraitImageView);
    }

    @Override
    public void onClick(View v) {
        if (groupInfo == null) {
            IMSipPhoneCtrl.getInstance().applyJoinGroup(groupId, ApplyGroupType.QR, "", imResponse -> {
                Log.e(TAG, "callback: " + imResponse.getCode());
                runOnUiThread(() -> Toast.makeText(GroupInfoActivity.this, "申请加群成功", Toast.LENGTH_SHORT).show());
            });
        } else {

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        SipCoreUtils.getInstance().removeImRcvGroupNotifyListener(this);
    }

    @Override
    public void onReceiveGroupNotify(GroupNotify message) {
        Log.e(TAG, "receiveGroupNotify: " + message.groupId + ",type = " + message.notifyType);
        if (message.notifyType == GroupNotifyType.GROUP_MEMBER_ADD) {
            ConversationActivity.startGroupConversation(this, groupId, imContactGroup.name);
        }
    }
}
